package com.sinoglobal.ningxia.activity.video;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;

/**
 * http://3g.nxtv.cn/fm/?chn=1061
 * 
 * @author temulu
 * @createdate 2015-10-21 上午10:31:27
 * @description 视频 http://3g.nxtv.cn/tv/?chn=wsh
 */
@SuppressLint("SetJavaScriptEnabled")
public class VideoActivity extends BaseActivity {
	private static final String TV_URL = "http://3g.nxtv.cn/tv/?chn=wsh";
	private View xCustomView;
	private WebView video;
	private CustomViewCallback xCustomViewCallback;
	private FrameLayout videoFullView;
	private MyWebChromeClient chromeClient;
//	private ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video);
		titleView.setText(R.string.ningxia_tv);

		openChenJInShi();//开启沉浸式
		video = (WebView) findViewById(R.id.wv_video);
		videoFullView = (FrameLayout) findViewById(R.id.video_follView);
		WebSettings settings = video.getSettings();
		settings.setBuiltInZoomControls(false);
		settings.setSupportZoom(false);
		settings.setDisplayZoomControls(false);
		settings.setUseWideViewPort(true);
		settings.setLoadWithOverviewMode(true);
		settings.setJavaScriptEnabled(true);
		settings.setPluginState(PluginState.ON);
		chromeClient = new MyWebChromeClient();
		video.setWebChromeClient(chromeClient);
//		dialog = ProgressDialog.show(this, null, "页面加载中，请稍后..");
		video.loadUrl(TV_URL);
//		dialog.show();
		video.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}

			@Override
			public void onPageFinished(WebView view, String url) {
//				dialog.dismiss();
			}
		});
	}

	private class MyWebChromeClient extends WebChromeClient {
		@Override
		public void onShowCustomView(View view, CustomViewCallback callback) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			video.setVisibility(View.INVISIBLE);
			if (xCustomView != null) {
				callback.onCustomViewHidden();
				return;
			}
			videoFullView.addView(view);
			xCustomView = view;
			xCustomViewCallback = callback;
			videoFullView.setVisibility(View.VISIBLE);
			titleRelativeLayout.setVisibility(View.GONE);
		}

		@Override
		public void onHideCustomView() {
			if (xCustomView == null) {
				return;
			}
			titleRelativeLayout.setVisibility(View.VISIBLE);
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			xCustomView.setVisibility(View.GONE);
			videoFullView.removeView(xCustomView);
			xCustomView = null;
			videoFullView.setVisibility(View.GONE);
			xCustomViewCallback.onCustomViewHidden();
			video.setVisibility(View.VISIBLE);
			video.reload();
		}

	}

	public boolean inCustomView() {
		return xCustomView != null;
	}

	/**
	 * 全屏时按返回键退出全屏方法
	 */
	public void hideCustomView() {
		chromeClient.onHideCustomView();
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}

	@Override
	protected void onResume() {
		super.onResume();
		video.onResume();
		video.resumeTimers();
		if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		video.onPause();
		video.pauseTimers();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			if (video.canGoBack()) {
				video.goBack();
				return true;
			} else if (inCustomView()) {
				hideCustomView();
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
//		if (dialog.isShowing()) {
//			dialog.dismiss();
//		}
		videoFullView.removeAllViews();
		video.stopLoading();
		video.setWebChromeClient(null);
		video.setWebViewClient(null);
		video.destroy();
	}
	@Override
	public void finish() {
		ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
		decorView.removeAllViews();
		super.finish();
	}
}
